﻿namespace Pub.Class
{
    using System;
    using System.Data;
    using System.IO;
    using System.Xml;

    public class Xml2
    {
        private XmlDocument objXmlDoc = new XmlDocument();
        private int state;
        private readonly string strXmlFile = string.Empty;

        public Xml2(string xmlFile)
        {
            this.strXmlFile = xmlFile;
            try
            {
                this.objXmlDoc.Load(xmlFile);
            }
            catch
            {
                this.state = 1;
            }
        }

        public void AddAttr(string node, string attributeName, string attributeValue)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this.state = 3;
            }
            else
            {
                XmlElement element = (XmlElement) this.objXmlDoc.SelectSingleNode(xpath);
                try
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (element.Attributes[strArray[i]].IsNull())
                        {
                            element.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                catch
                {
                    this.state = 4;
                }
            }
        }

        public void AddNode(string mainNode, string node, string attributeName, string attributeValue)
        {
            string xpath = mainNode.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this.state = 3;
            }
            else
            {
                XmlNode node2 = this.objXmlDoc.SelectSingleNode(xpath);
                XmlElement newChild = this.objXmlDoc.CreateElement(node);
                if (attributeName.Trim() != "")
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (node2.Attributes[strArray[i]].IsNull())
                        {
                            newChild.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                node2.AppendChild(newChild);
            }
        }

        public void AddNode(string mainNode, string node, string attributeName, string attributeValue, string content)
        {
            string xpath = mainNode.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this.state = 3;
            }
            else
            {
                XmlNode node2 = this.objXmlDoc.SelectSingleNode(xpath);
                XmlElement newChild = this.objXmlDoc.CreateElement(node);
                if (attributeName.Trim() != "")
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (node2.Attributes[strArray[i]].IsNull())
                        {
                            newChild.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                newChild.InnerText = content;
                node2.AppendChild(newChild);
            }
        }

        public void Close()
        {
            if (this.objXmlDoc.IsNotNull())
            {
                this.objXmlDoc = null;
            }
        }

        public static bool Create(string xmlFile, string cssFile, string xlsFile, string encoding, string node)
        {
            if (node.Trim().Equals(""))
            {
                return false;
            }
            if (encoding.Trim().Equals(""))
            {
                encoding = "utf-8";
            }
            string str = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
            if (!cssFile.Trim().Equals(""))
            {
                string str2 = str;
                str = str2 + Environment.NewLine + "<?xml-stylesheet type=\"text/css\" href=\"" + cssFile + "\"?>";
            }
            if (!xlsFile.Trim().Equals(""))
            {
                string str3 = str;
                str = str3 + Environment.NewLine + "<?xml-stylesheet type=\"text/xsl\" href=\"" + xlsFile + "\" media=\"screen\"?>";
            }
            str = str + Environment.NewLine + node;
            return Log.Write(xmlFile, str, encoding);
        }

        public void DelNode(string mainNode)
        {
            string xpath = mainNode.TrimEnd(new char[] { '/' });
            XmlNode node = this.objXmlDoc.SelectSingleNode(xpath);
            if (node.IsNotNull())
            {
                node.ParentNode.RemoveChild(node);
            }
        }

        public string GetAttr(string node, string attributeName)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            string str2 = "";
            string[] strArray = attributeName.Split(new char[] { '|' });
            XmlNode node2 = this.objXmlDoc.SelectSingleNode(xpath);
            for (int i = 0; i <= (strArray.Length - 1); i++)
            {
                try
                {
                    str2 = str2 + node2.Attributes[strArray[i]].Value.ToString() + "|";
                }
                catch
                {
                    str2 = str2 + "|";
                }
            }
            return str2.Substring(0, str2.Length - 1);
        }

        public DataSet GetData()
        {
            DataSet set = new DataSet();
            set.ReadXml(this.strXmlFile);
            return set;
        }

        public DataSet GetData(string node)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            DataSet set = new DataSet();
            StringReader reader = new StringReader(this.objXmlDoc.SelectSingleNode(xpath).OuterXml);
            set.ReadXml(reader);
            return set;
        }

        public string GetNodeText(string node)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            return this.objXmlDoc.SelectSingleNode(xpath).InnerText;
        }

        public void Save()
        {
            try
            {
                if (this.state == 0)
                {
                    this.objXmlDoc.Save(this.strXmlFile);
                }
            }
            catch
            {
                this.state = 2;
            }
            this.objXmlDoc = null;
        }

        public void SetAttr(string node, string attributeName, string attributeValue)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this.state = 3;
            }
            else
            {
                XmlNode node2 = this.objXmlDoc.SelectSingleNode(xpath);
                for (int i = 0; i <= (strArray.Length - 1); i++)
                {
                    try
                    {
                        node2.Attributes[strArray[i]].Value = strArray2[i];
                    }
                    catch
                    {
                    }
                }
            }
        }

        public void SetNodeText(string node, string content)
        {
            string xpath = node.TrimEnd(new char[] { '/' });
            this.objXmlDoc.SelectSingleNode(xpath).InnerText = content;
        }

        public string ToXmlText()
        {
            return this.objXmlDoc.OuterXml;
        }

        public int State
        {
            get
            {
                return this.state;
            }
        }
    }
}

